﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using TrainingManagement.api.Models.BusinessLogic;
using TrainingManagement.api.Models.BusinessLogic.Position;
using TrainingManagement.api.Models.BusinessObjects;

namespace TrainingManagement.api.Controllers
{
    public class PositionController : ApiController
    {
        public Position PutPosition(Position position)
        {
            return new PositionCreateOperation().Run(position);
        }

        [System.Web.Http.ActionName("Update")]
        public void PostUpdatePosition(Position position)
        {
            new PositionUpdateOperation().Run(position);
        }

        [System.Web.Http.ActionName("SkillSets")]
        public Position PostPositionSkillSets([FromUri]int positionId, [FromBody]List<int> skillSets)
        {
            if (skillSets.Any(a => a == 0)) throw new Exception("Skill set IDs must be specified if you wish to associate them with a position.");
            var dtos = skillSets.Select(set => new PositionSkillSetDto
                {
                    PositionId = positionId, SkillSetId = set
                }).ToList();
            return new PositionSkillSetItemInsertOperation().Run(dtos);
        }

        public void DeletePosition(int positionId)
        {
            new PositionDeleteOperation().Run(positionId);
        }

        public List<Position> GetPosition(int? positionId, bool loadSkillSets = false)
        {
            return new PositionSelectOperation().Run(positionId, loadSkillSets);
        } 

    }
}
